Closed-form marginal data density of VARs ========================================= Under a natural-conjugate prior the coefficients and the residual covariance of a reduced-form VAR integrate out analytically. When ``estimate`` finds the posterior mode of such a VAR in closed form (see *How the mode is found* in :doc:`../ModelShapes/Main Reduced form VAR Modeling`), it also reports the exact log marginal data density, next to the Laplace approximation:: pm = mdlest.model_data.estimation.posterior_maximization; pm.log_marginal_data_density_closed_form % log p(Y), in nats pm.log_marginal_data_density_closed_form_reason % '', a label or a reason No option turns it on. It is available for ``rfvar_model`` and ``prfvar_model`` with constant parameters, and for switching VARs whose regimes are known from the data (:doc:`Switching VARs with known regimes`). .. contents:: :local: :depth: 1 The prior and the integral -------------------------- Write the scored observations of a VAR with :math:`n` variables as .. math:: Y = X B + U, \qquad \mathrm{vec}(B) \mid \Sigma \sim N\left(\mathrm{vec}(B_{0}), \Sigma \otimes \Omega\right), \qquad \Sigma \sim IW(S, \nu), with :math:`Y` the :math:`T \times n` observations, :math:`X` the :math:`T \times K` regressors (the constant, the deterministic variables, then lags 1 to :math:`p` of every variable) and the rows of :math:`U` independent :math:`N(0, \Sigma)`. ``niw`` is this prior, with :math:`B_{0}`, :math:`\Omega`, :math:`S` and :math:`\nu` in the fields ``B_prior``, ``Omega``, ``scale_matrix`` and ``df`` of the template. ``generate_sims_zha_prior`` is of the same form when it has its prior on :math:`\Sigma` (``use_sigma_prior = true``, the default); its dummy observations give the four terms. With .. math:: K_{A} = X'X + \Omega^{-1}, \qquad \hat{B} = K_{A}^{-1}\left(X'Y + \Omega^{-1}B_{0}\right), \qquad \hat{S} = S + Y'Y + B_{0}'\Omega^{-1}B_{0} - \hat{B}'K_{A}\hat{B}, the posterior is :math:`\mathrm{vec}(B) \mid \Sigma, Y \sim N(\mathrm{vec}(\hat{B}), \Sigma \otimes K_{A}^{-1})`, :math:`\Sigma \mid Y \sim IW(\hat{S}, \nu + T)`, and .. math:: \log p(Y) = -\frac{nT}{2}\log\pi - \frac{n}{2}\left(\log|\Omega| + \log|K_{A}|\right) + \frac{\nu}{2}\log|S| - \frac{\nu + T}{2}\log|\hat{S}| + \log\Gamma_{n}\left(\frac{\nu + T}{2}\right) - \log\Gamma_{n}\left(\frac{\nu}{2}\right). What the number is ------------------ * :math:`p(Y)` is the density of the scored observations given the observations that are not scored: the first ``lag_length`` observations of the estimation window, or the first ``kf_presample`` when that is larger. It integrates the kernel that ``estimate`` maximizes, likelihood times prior, without the truncations described below, so that at every parameter value :math:`\theta` .. math:: \log p(Y) = \log p(Y \mid \theta) + \log p(\theta) - \log p(\theta \mid Y). * It is a natural log, in nats, of a density of the data in their own units. Two values compare models only on the same data, in the same units, and on the same scored dates; their difference is then a log Bayes factor. * ``log_post`` is the log posterior kernel at the mode, a value at one point: it is not a score of the model. * The Laplace approximation, ``log_marginal_data_density_laplace``, approximates the same integral from the mode and the Hessian. With ``estim_hessian_type = 'none'`` it is not computed; the closed form still is. * A regression without observations has :math:`\log p(Y) = 0`. Under switching with known regimes, the value is the sum of this closed form over the blocks of dates that share a covariance (see :doc:`Switching VARs with known regimes`). Which priors have it -------------------- .. list-table:: :header-rows: 1 :widths: 34 66 * - ``estim_var_prior`` - ``log_marginal_data_density_closed_form`` * - ``niw`` - the value * - ``generate_sims_zha_prior`` (``use_sigma_prior = true``, the default) - the value * - ``generate_sims_zha_prior`` with ``use_sigma_prior = false`` - NaN; the reason is ``no marginal likelihood: the prior has no proper density on the covariance`` * - ``minnesota``, ``inw`` - NaN; the reason is ``no closed-form marginal likelihood: a proper matrix-normal/IW integral is required; the coefficient prior is independent of the covariance`` * - ``user_defined`` - empty: the mode is searched numerically The value is also NaN, with the reason, when the degrees of freedom of the inverse-Wishart do not exceed :math:`n - 1`, when :math:`\Omega^{-1}` or :math:`S` is not positive definite, and when :math:`\hat{S}` is not. The field is empty (``[]``) whenever the mode is searched numerically: ``estim_var_closed_form = false``, linear restrictions under ``niw`` or Sims-Zha, and the other cases listed under *How the mode is found* in :doc:`../ModelShapes/Main Reduced form VAR Modeling` (priors in ``estim_priors``, endogenous priors, missing observations, a closed-form mode that the posterior kernel rejects, and so on). ``estimate`` then prints ``Posterior mode by numerical search (no closed form: ...)`` with the reason, except for a template without a closed form (``user_defined``) and for ``estim_var_closed_form = false``; a rejected mode prints ``Posterior mode by numerical search: the kernel rejects the closed-form mode (...)``. Truncation: the label in the reason ----------------------------------- With RISE's defaults the kernel that ``estimate`` maximizes is truncated twice: * ``solve_check_stability = true`` sets it to zero at VARs that are not stable; * ``estim_prior_trunc = 1e-10`` bounds each parameter by quantiles at ``1e-10/2`` and ``1 - 1e-10/2``: those of its prior or, for a parameter of the VAR prior, those of the wide normal that carries its bounds (about :math:`6.5 \times 10^{8}` on either side of zero). The closed form integrates the untruncated kernel. RISE reports it and labels it in ``log_marginal_data_density_closed_form_reason``, with one of .. code-block:: none integral of the untruncated kernel; the estimated kernel is truncated by the stability check integral of the untruncated kernel; the estimated kernel is truncated by finite parameter bounds integral of the untruncated kernel; the estimated kernel is truncated by the stability check and finite parameter bounds and ``estimate`` prints:: Closed-form log marginal data density (untruncated kernel) = -70.107321 integral of the untruncated kernel; the estimated kernel is truncated by the stability check and finite parameter bounds With ``solve_check_stability = false`` and ``estim_prior_trunc = 0``, and no other finite bound on a parameter, the value is the exact integral of the estimated kernel, the reason is empty, and ``estimate`` prints:: Closed-form log marginal data density (exact, conjugate VAR prior) = -70.107321 The reason of a reported value is empty exactly when the value is that exact integral. The number itself does not depend on the truncation: in the example below it is the same to the last digit with and without the defaults. If :math:`A` is the region the truncation keeps, the marginal data density of the model whose prior is truncated to :math:`A` (and renormalized) is :math:`\log p(Y) + \log P(A \mid Y) - \log P(A)`; RISE does not compute these two probabilities. An example ---------- A VAR(2) in quarterly inflation and the federal funds rate (the US data of Smets and Wouters (2007) in RISE's ``examples/VariousModels/SmetsWouters``; ``pinfobs`` and ``robs``, both in percent per quarter), estimated from 1966Q1 to 2004Q4 under ``niw`` with its default hyperparameters:: prior = rise.engine.var.var_priors.niw({'pinfobs','robs'}, true, {}, 2, db); mdlest = estimate(m, data = db, ... estim_start_date = date2serial('1966Q1'), ... estim_end_date = date2serial('2004Q4'), ... estim_var_prior = prior, ... solve_check_stability = false, ... estim_prior_trunc = 0); ``niw`` scales its prior by univariate AR(2) fits on the data it is given, here all of ``db``, the estimation window included; the closed form holds that scale fixed. To keep the scored quarters out of the prior, give ``niw`` a pre-sample. The 154 scored quarters (1966Q3-2004Q4) give .. list-table:: :header-rows: 1 :widths: 60 40 * - Quantity (nats) - Value * - ``log_marginal_data_density_closed_form`` - -70.107321 * - ``log_marginal_data_density_laplace`` - -70.384232 * - log likelihood at the mode (``log_lik``) - -38.860659 * - log prior at the mode (``log_prior``) - 6.285893 * - log posterior density at the mode, from :math:`\hat{B}`, :math:`K_{A}`, :math:`\hat{S}`, :math:`\nu + T` - 37.532555 The last three give :math:`-38.860659 + 6.285893 - 37.532555 = -70.107321`, the closed form to :math:`10^{-13}`; the Laplace approximation is 0.28 nats below it. To choose the lag length, ``kf_presample = 4`` makes the VARs with 1 to 4 lags score the same 152 quarters (1967Q1-2004Q4). With equal prior odds: .. list-table:: :header-rows: 1 :widths: 20 40 40 * - Lags - log marginal data density - Posterior probability * - 1 - -72.8894 - 0.059 * - 2 - -71.1826 - 0.324 * - 3 - -71.2615 - 0.299 * - 4 - -71.1999 - 0.318 On the VAR(2), ``generate_sims_zha_prior`` with its defaults gives -55.8205; ``minnesota``, ``inw`` and Sims-Zha without its prior on :math:`\Sigma` give NaN with the reasons in the table above. The tutorial ``Estimation/var_closed_form_mdd/howto.m`` runs this example. The routines ------------ ``estimate`` calls ``rise.engine.var.vartools.bvar_posterior_mode``, whose output carries ``log_marginal_data_density`` and ``log_marginal_data_density_reason``. The value comes from ``P.log_marginal_likelihood()`` of ``rise.engine.var.vartools.bvar_conjugate_problem``, which returns ``[lml, why]``. Both take the model data that ``pull_objective`` returns as its seventh output. The closed form of one regression is:: [lml, why] = rise.engine.var.vartools.conjugate_log_marginal_likelihood(Y, X, Bbar, Oinv, S, nu) with ``Y`` the :math:`n \times T` observations, ``X`` the :math:`K \times T` regressors, ``Bbar`` the :math:`n \times K` prior mean (``B_prior.'``), ``Oinv`` :math:`\Omega^{-1}`, and ``S``, ``nu`` the inverse-Wishart.